package com.laolang.pluto.common.web.jackson;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author laolang
 * @date 2021-08-15 06:21
 */
public class LocalTimeDeserialize extends JsonDeserializer<LocalTime> {
    private final DateTimeFormatter formatter;

    public LocalTimeDeserialize() {
        this.formatter = DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN);
    }

    @Override
    public LocalTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String text = p.getText();
        if (StrUtil.isNotBlank(text)) {
            return LocalTime.parse(text, formatter);
        }
        return null;
    }
}
